package com.insleton.admin.config;

import com.vteba.tx.jdbc.spring.SpringJdbcTemplate;
import com.vteba.utils.id.IdsGenerator;
import com.vteba.web.config.SecretKeyProperties;
import com.vteba.web.filter.DefaultContextFilter;
import com.vteba.web.filter.XssDefenderFilter;
import com.vteba.web.filter.jwt.Default2TokenService;
import com.vteba.web.filter.jwt.TokenService;
import com.vteba.web.servlet.converter.binary.ProtobufGenericHttpMessageConverter;
import jakarta.inject.Inject;
import jakarta.servlet.DispatcherType;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.GenericHttpMessageConverter;

import javax.sql.DataSource;

@Configuration
public class SpringConfiguration {

    @Bean
    public IdsGenerator idsGenerator() {
        return new IdsGenerator();
    }

    @Bean
    @Inject // 没有加这个注解，DataSource居然也注入进来了
    public SpringJdbcTemplate springJdbcTemplate(DataSource dataSource) {
        return new SpringJdbcTemplate(dataSource);
    }

    @Bean
    public SecretKeyProperties secretKeyProperties() {
        SecretKeyProperties props = new SecretKeyProperties();
        props.setPublicKey("MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtf65gk-mqFFlvck7tVFPHoiAEkcp__TsYP8KertOekebUOk2spWUZyAh8e6vSeDJ2qeqFZRsypt3VV3EgOmQDgCGC6Wj5R0dpAb36O1R0X1MHavzdbjqPq4oVTr-qwrrS7d3EpRSbS25Vbur2_DTrfDSVSJ4271CW_k2-9DzuwSQ2fN9QpCzWXQXSztCMCo456vJKmj76vCaNVKv97vrGAM5rb8l1UeXlkiFnZSPc9-ZLGcTYgxa9ks3cAQr0F2F9ITu9Tzs8TEcKu3LzL2ZhHOsrLSaY7f1pMZsGS1Jw7CBY8oPlrGwiFVMTfrd2bZBeRiNCHvsxoj2ipMZzJxHenNq2WKQS2TNAK5cH038bvngcOSYoSEDc4Xq1WZLbTTY5xZUibWKMJKnR3qFaDMI9tvswW_zcgqoLRH553DxdynJWK2tp2Tc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HCXdTAtBSj4wwdlHruSTpBXGvWi4UOSaRndaR6Uc3drQNpdfN5jcBxZnT0cyQKIJs5limdqaAennZkGfBfI_yvh96NRv4HJT3fT-EWsGcM8tFVQg2uCsm0I7SCqaxldhrrK8PQ-Eo7NG9lNOJRfTAPHQUl8DiHirpvs7FgSRTaHS_lpIKA7yxjpqH5NAMrG5_A");
        props.setSalt("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIhvaW0B5GzZafRH9TGqiroLl_q5a7z0gJnL8AO5gMeymrFGUV_jcEQ3BKkWZPXK6z-2E_dPBtiuvFBYRE9s5yUCAwEAAQ");
        return props;
    }

    @Bean
    public TokenService defaultJwtService() {
        return new Default2TokenService();
    }

    // @Bean
    // public GenericHttpMessageConverter<Object> httpMessageConverter() {
    //     ProtobufGenericHttpMessageConverter converter = new ProtobufGenericHttpMessageConverter();
    //     converter.setFeign(true);
    //     return converter;
    // }
    //
    // @Bean
    // public FilterRegistrationBean<XssDefenderFilter> xssFilter() {
    //     XssDefenderFilter xssDefenderFilter = new XssDefenderFilter();
    //     FilterRegistrationBean<XssDefenderFilter> filter = new FilterRegistrationBean<>(xssDefenderFilter);
    //     filter.addUrlPatterns("/*");
    //     filter.setDispatcherTypes(DispatcherType.REQUEST); // 指定拦截的类型，否则连forward也会拦截
    //     return filter;
    // }
    //
    // @Bean
    // public FilterRegistrationBean<DefaultContextFilter> contextFilter() {
    //     DefaultContextFilter contextFilter = new DefaultContextFilter();
    //     // 2024-7-14 nginx proxy, set to ture
    //     contextFilter.setAllowOrigin(true); // 如果在网关中进行了跨域处理，这里就不需要设置。2024-1-29 22:14
    //     FilterRegistrationBean<DefaultContextFilter> filter = new FilterRegistrationBean<>(contextFilter);
    //     // 没有配置这个头部字段，导致跨域验证失败。现在在filter中默认添加了。
    //     filter.addInitParameter("allowHeaders", "timestamp, x-token");
    //     // filter.addInitParameter("xFrameOptions", "allowall"); // 不配置就是允许所有
    //     filter.addUrlPatterns("/*");
    //     filter.setDispatcherTypes(DispatcherType.REQUEST);
    //     return filter;
    // }
}
